package com.mdz.myapplication.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdz.myapplication.Entity.Fileloc;
import com.mdz.myapplication.Mapper.FilelocMapper;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author lake957
 */
@FXMLController
public class FilelocContorller implements Initializable {

	@Autowired
	private FilelocMapper filelocMapper;

	@FXML
	private TextField ckml;

	@FXML
	private TextField rkml;

	@Value(value = "${gsbh}")
	private String gsbh;

	@FXML
	private Button qdbtn;

	@FXML
	private Button qxbtn;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		System.out.println(gsbh);
		List<Fileloc> ck=filelocMapper.getlistbyck(gsbh);
		List<Fileloc> rk=filelocMapper.getlistbyrk(gsbh);
		if (ck.size()>0){
			ckml.setText(ck.get(0).getFileloc());
		}else {
			ckml.setText("");
		}
		if (rk.size()>0){
			rkml.setText(rk.get(0).getFileloc());
		}else {
			rkml.setText("");
		}
	}

	@FXML
	private void setFileLocforck() {
		Stage stage=new Stage();
//		FileChooser fileChooser = new FileChooser();
//		fileChooser.setTitle("Open Resource File");
//		fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))
//		);
//		fileChooser.getExtensionFilters().addAll(
//				new FileChooser.ExtensionFilter("All Images", "*.*"),
//				new FileChooser.ExtensionFilter("JPG", "*.jpg"),
//				new FileChooser.ExtensionFilter("GIF", "*.gif"),
//				new FileChooser.ExtensionFilter("BMP", "*.bmp"),
//				new FileChooser.ExtensionFilter("PNG", "*.png")
//		);
//		File file = fileChooser.showOpenDialog(stage);
//		System.out.println(file);
		DirectoryChooser directoryChooser=new DirectoryChooser();
		File file = directoryChooser.showDialog(stage);
		String path = file.getPath();//选择的文件夹路径
		System.out.println(path);
		ckml.setText(path);
	}

	@FXML
	private void setFileLocforrk() {
		Stage stage=new Stage();
		DirectoryChooser directoryChooser=new DirectoryChooser();
		File file = directoryChooser.showDialog(stage);
		String path = file.getPath();//选择的文件夹路径
		System.out.println(path);
		rkml.setText(path);
	}

	@FXML
	private void qurenbtn() {
		String rkdz = rkml.getText().trim();
		String ckdz = ckml.getText().trim();
		Alert alert = new Alert(Alert.AlertType.INFORMATION);
		alert.setHeaderText("温馨提示");
		if (rkdz.isEmpty() && ckdz.isEmpty()) {
			alert.setContentText("出库目录和入库目录不能为空！");
			alert.showAndWait();
			return;
		} else if (rkdz.isEmpty()) {
			alert.setContentText("入库目录不能为空！");
			alert.showAndWait();
			return;
		} else if (ckdz.isEmpty()) {
			alert.setContentText("出库目录不能为空！");
			alert.showAndWait();
			return;
		}
		List<Fileloc> ck=filelocMapper.getlistbyck(gsbh);
		List<Fileloc> rk=filelocMapper.getlistbyrk(gsbh);
		if (ck.size()>0&&rk.size()>0){
			if (ck.get(0).getFileloc().equals(ckdz)&&rk.get(0).getFileloc().equals(rkdz)){
				alert.setContentText("出库目录和入库目录未进行修改！");
				alert.showAndWait();
				return;
			}else {
				//			更新
				Fileloc filelocrk = new Fileloc();
				filelocrk.setFileloc(rkdz).setGsbh(gsbh).setLeix("入库").setId(rk.get(0).getId());
				System.out.println(filelocrk);
				filelocMapper.updateById(filelocrk);
				Fileloc filelocck = new Fileloc();
				filelocck.setFileloc(ckdz).setGsbh(gsbh).setLeix("出库").setId(ck.get(0).getId());
				System.out.println(filelocck);
				filelocMapper.updateById(filelocck);
				Stage stage = (Stage)qdbtn.getScene().getWindow();
				stage.close();
			}
		}else {
//			插入
			Fileloc filelocrk = new Fileloc();
			filelocrk.setFileloc(rkdz).setGsbh(gsbh).setLeix("入库");
			System.out.println(filelocrk);
			filelocMapper.insert(filelocrk);
			Fileloc filelocck = new Fileloc();
			filelocck.setFileloc(ckdz).setGsbh(gsbh).setLeix("出库");
			System.out.println(filelocck);
			filelocMapper.insert(filelocck);
			Stage stage = (Stage)qdbtn.getScene().getWindow();
			stage.close();
		}
//
	}

	@FXML
	private void quxiaobtn() {
		Stage stage = (Stage)qxbtn.getScene().getWindow();
		stage.close();
	}

}
